package com.hh.planCategory.controller;/*
 * @Auther:ZhengXueCheng
 * @Date:2023/7/25
 * @VERSON:1.8
 */

import com.hh.planCategory.vo.AddPlanCategoryVo;
import com.hh.planCategory.vo.PlanCategoryVo;
import com.hh.planCategory.vo.StatusChangeVo;
import com.hh.planCategory.vo.UpdatePlanCategoryVo;
import com.hh.planCategory.service.PlanCategoryService;
import com.hh.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/planCategory")
@CrossOrigin
public class PlanCategoryController {
    @Autowired
    private PlanCategoryService planCategoryService;

    @PostMapping("/queryPage")
    public ResultVo queryPage(@RequestBody PlanCategoryVo planCategoryVo) {
        return ResultVo.success(planCategoryService.findAllByPage(planCategoryVo));
    }

    @PostMapping("/updateStatus")
    public ResultVo updateStatus(@RequestBody StatusChangeVo statusChangeVo) {
        if (planCategoryService.updateStatus(statusChangeVo)) {
            return ResultVo.success();
        }
        return ResultVo.error("更改失败");
    }

    @PostMapping("/addPlanCategory")
    public ResultVo addPlanCategory(@RequestBody @Valid AddPlanCategoryVo addPlanCategoryVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return planCategoryService.addPlanCategory(addPlanCategoryVo);
    }

    @PostMapping("/updatePlanCategory")
    public ResultVo updatePlanCategory(@RequestBody UpdatePlanCategoryVo updatePlanCategoryVo) {
        if (planCategoryService.updatePlanCategory(updatePlanCategoryVo)) {
            return ResultVo.success();
        }
        return ResultVo.error("更新失败");
    }

    /**
     * 计划大类下拉列表
     */
    @GetMapping("/planCategoryAll")
    public ResultVo planCategoryAll(){
        return planCategoryService.getAllPlanCategoryList();
    }

}
